package com.hzpd.hainan.module.news.adapter;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hzpd.hainan.R;
import com.hzpd.hainan.app.ListBaseAdapter;
import com.hzpd.hainan.model.news.NewsBean;
import com.hzpd.hainan.utils.LogUtils;
import com.hzpd.hainan.utils.TUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 猪猪侠 on 2017/7/3.
 */

public class NewsItemListViewAdapter extends ListBaseAdapter<NewsBean> {
    public static enum Itemtype {
        THREEPIC(0), LEFTPIC(1), NOPIC(2), BIGPIC(3);
        private int type;

        private Itemtype(int type) {
            this.type = type;
        }

        public int getType() {
            return type;
        }
    }


    public NewsItemListViewAdapter(Activity context) {
        super(context);
    }
    @Override
    public int getViewTypeCount() {
        return Itemtype.values().length;
    }

    @Override
    public int getItemViewType(int position) {
        NewsBean nb = list.get(position);
        if ("4".equals(nb.getType())) {
            return Itemtype.THREEPIC.getType();
        }
        if ("1".equals(nb.getType())) {
            return Itemtype.NOPIC.getType();
        }
        if ("5".equals(nb.getType())) {
            return Itemtype.BIGPIC.getType();
        } else {
            return Itemtype.LEFTPIC.getType();
        }
    }
    @Override
    public View getMyView(int position, View convertView, ViewGroup parent) {
        VHThree vhThree = null;
        VHLeftPic vhLeftPic = null;
        VHNOPic vhnoPic = null;
        VHBigPic vhBigPic = null;
        int type = getItemViewType(position);
        //数据设置
        final NewsBean nb = list.get(position);
        if (convertView == null) {
            switch (type) {
                case 0: {
                    convertView = inflater.inflate(R.layout.news_3_item_layout, parent, false);
                    vhThree = new VHThree(convertView);
                    convertView.setTag(vhThree);
                }
                break;
                case 1: {
                    convertView = inflater.inflate(R.layout.news_list_item_layout, parent, false);
                    vhLeftPic = new VHLeftPic(convertView);
                    convertView.setTag(vhLeftPic);
                }
                break;
                case 2: {
                    convertView = inflater.inflate(R.layout.news_list_item_nopic_layout, parent, false);
                    vhnoPic = new VHNOPic(convertView);
                    convertView.setTag(vhnoPic);
                }
                break;
                case 3: {
                    convertView = inflater.inflate(R.layout.news_list_bigitem_layout, parent, false);
                    vhBigPic = new VHBigPic(convertView);
                    convertView.setTag(vhBigPic);
                }
                break;
            }
        }else {
            switch (type) {
                case 0: {
                    vhThree = (VHThree) convertView.getTag();
                }
                break;
                case 1: {
                    vhLeftPic = (VHLeftPic) convertView.getTag();
                }
                break;
                case 2: {
                    vhnoPic = (VHNOPic) convertView.getTag();
                }
                break;
                case 3: {
                    vhBigPic = (VHBigPic) convertView.getTag();
                }
                break;
            }
        }
        switch (type) {
            case 0: {
                vhThree.newsitem_title.setText(nb.getTitle());
                vhThree.tv3.setText(nb.getUpdateTime());
                List<String> s = nb.getImgs();
                if (s.size() == 1) {
                    Glide.with(context)
                            .load(s.get(0))
                            .crossFade()
                            .placeholder(R.drawable.default_bg)
                            .into(vhThree.img0);
                    Glide.with(context).load(s.get(0)).crossFade()
                            .placeholder(R.drawable.default_bg).into(vhThree.img0);
                } else if (s.size() == 2) {
                    Glide.with(context).load(s.get(0)).crossFade()
                            .placeholder(R.drawable.default_bg).into(vhThree.img0);
                    Glide.with(context).load(s.get(1)).crossFade()
                            .placeholder(R.drawable.default_bg).into(vhThree.img1);
                } else if (s.size() > 2) {
                    Glide.with(context).load(s.get(0)).crossFade()
                            .placeholder(R.drawable.default_bg).into(vhThree.img0);
                    Glide.with(context).load(s.get(1)).crossFade()
                            .placeholder(R.drawable.default_bg).into(vhThree.img1);
                    Glide.with(context).load(s.get(2)).crossFade()
                            .placeholder(R.drawable.default_bg).into(vhThree.img2);
                }
                vhThree.praisenumber_id.setText(nb.getPraisenum());
                vhThree.viewnumber__id.setText(nb.getViewnum());
                vhThree.newsitem_foot.setVisibility(View.GONE);
                //置顶 ,投票,专题
                if (nb.getIstop() != null && nb.getIstop().equals("1")) {
                    vhThree.nli_foot.setImageResource(R.drawable.list_special);
                    vhThree.nli_foot.setVisibility(View.VISIBLE);
                } else if (nb.getVoteflag() != null && nb.getVoteflag().equals("1")) {
                    vhThree.nli_foot.setImageResource(R.drawable.list_vote);
                    vhThree.nli_foot.setVisibility(View.VISIBLE);
                } else if ("4".equals(nb.getRtype())) {
                    vhThree.nli_foot.setImageResource(R.drawable.zq_subscript_special);
                    vhThree.nli_foot.setVisibility(View.VISIBLE);
                }
            }
            break;
            case 1: {
                vhLeftPic.newsitem_title.setText(nb.getTitle());
                vhLeftPic.newsitem_copyfrom.setText(nb.getCopyfrom());
                vhLeftPic.praisenumber_id.setText(nb.getPraisenum());
                vhLeftPic.viewnumber__id.setText(nb.getViewnum());
                vhLeftPic.newsitem_time.setText(nb.getUpdateTime());
                vhLeftPic.newsitem_img.setVisibility(View.VISIBLE);
                vhLeftPic.nli_foot.setVisibility(View.GONE);
                if ("1".equals(nb.getType())) {
                    vhLeftPic.newsitem_img.setVisibility(View.GONE);
                }
                if (nb.getIstop() != null && nb.getIstop().equals("1")) {
                    vhLeftPic.nli_foot.setImageResource(R.drawable.list_special);
                    vhLeftPic.nli_foot.setVisibility(View.VISIBLE);
                } else if (nb.getVoteflag() != null && nb.getVoteflag().equals("1")) {
                    vhLeftPic.nli_foot.setImageResource(R.drawable.list_vote);
                    vhLeftPic.nli_foot.setVisibility(View.VISIBLE);
                } else if ("4".equals(nb.getRtype())) {
                    vhLeftPic.nli_foot.setImageResource(R.drawable.zq_subscript_special);
                    vhLeftPic.nli_foot.setVisibility(View.VISIBLE);
                }
                if (vhLeftPic.newsitem_img.getVisibility() == View.VISIBLE
                        && null != nb.getImgs()
                        && nb.getImgs().size() > 0) {
                    Glide.with(context).load(nb.getImgs().get(0)).crossFade()
                            .placeholder(R.drawable.default_bg).into(vhLeftPic.newsitem_img);
                } else {
                    Glide.with(context).load("").crossFade()
                            .placeholder(R.drawable.default_bg).into(vhLeftPic.newsitem_img);
                }
                final ImageView imageView = vhLeftPic.tv_praise_id;
                final TextView textView = vhLeftPic.praisenumber_id;
                vhLeftPic.ll_praise_id.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        addCollection(nb, imageView,textView,Integer.parseInt(nb.getPraisenum()));
                        TUtils.toast("praise");
                        LogUtils.i("praisenum-->"+nb.getPraisenum());
                    }
                });

            }
            break;
            case 2: {
                vhnoPic.newsitem_title.setText(nb.getTitle());
                vhnoPic.newsitem_copyfrom.setText(nb.getCopyfrom());
                vhnoPic.praisenumber_id.setText(nb.getPraisenum());
                vhnoPic.viewnumber__id.setText(nb.getViewnum());
                vhnoPic.newsitem_time.setText(nb.getUpdateTime());
                vhnoPic.nli_foot.setVisibility(View.GONE);
                //置顶 ,投票,专题
                if (nb.getIstop() != null && nb.getIstop().equals("1")) {
                    vhnoPic.nli_foot.setImageResource(R.drawable.list_special);
                    vhnoPic.nli_foot.setVisibility(View.VISIBLE);
                } else if (nb.getVoteflag() != null && nb.getVoteflag().equals("1")) {
                    vhnoPic.nli_foot.setImageResource(R.drawable.list_vote);
                    vhnoPic.nli_foot.setVisibility(View.VISIBLE);
                } else if ("4".equals(nb.getRtype())) {
                    vhnoPic.nli_foot.setImageResource(R.drawable.zq_subscript_special);
                    vhnoPic.nli_foot.setVisibility(View.VISIBLE);
                }
            }
            break;
            case 3: {
                vhBigPic.newsitem_title.setText(nb.getTitle());
                vhBigPic.newsitem_copyfrom.setText(nb.getCopyfrom());

                vhBigPic.praisenumber_id.setText(nb.getPraisenum());
                vhBigPic.viewnumber__id.setText(nb.getViewnum());
                vhBigPic.newsitem_time.setText(nb.getUpdateTime());

                vhBigPic.newsitem_img.setVisibility(View.VISIBLE);
                vhBigPic.nli_foot.setVisibility(View.GONE);
                if ("1".equals(nb.getType())) {
                    vhBigPic.newsitem_img.setVisibility(View.GONE);
                }
                if (nb.getIstop() != null && nb.getIstop().equals("1")) {
                    vhBigPic.nli_foot.setImageResource(R.drawable.list_special);
                    vhBigPic.nli_foot.setVisibility(View.VISIBLE);
                } else if (nb.getVoteflag() != null && nb.getVoteflag().equals("1")) {
                    vhBigPic.nli_foot.setImageResource(R.drawable.list_vote);
                    vhBigPic.nli_foot.setVisibility(View.VISIBLE);
                } else if ("4".equals(nb.getRtype())) {
                    vhBigPic.nli_foot.setImageResource(R.drawable.zq_subscript_special);
                    vhBigPic.nli_foot.setVisibility(View.VISIBLE);
                }
                if (vhBigPic.newsitem_img.getVisibility() == View.VISIBLE
                        && null != nb.getImgs()
                        && nb.getImgs().size() > 0) {
                    Glide.with(context).load(nb.getImgs().get(0)).crossFade()
                            .placeholder(R.drawable.default_bg).into(vhBigPic.newsitem_img);
                } else {
                    Glide.with(context).load("").crossFade()
                            .placeholder(R.drawable.default_bg).into(vhBigPic.newsitem_img);
                }

            }
            break;
        }
        return convertView;
    }
    //三联图
    static class VHThree {
        @BindView(R.id.newsitem_title)
        TextView newsitem_title;
        @BindView(R.id.news_3_tv_time)
       TextView tv3;
        @BindView(R.id.nli_foot)
        ImageView nli_foot;
        @BindView(R.id.news_3_item1)
         ImageView img0;
        @BindView(R.id.news_3_item2)
         ImageView img1;
        @BindView(R.id.news_3_item3)
         ImageView img2;
        @BindView(R.id.newsitem_foot)
         ImageView newsitem_foot;
        @BindView(R.id.newsitem_comments)
         TextView newsitem_comments;
        @BindView(R.id.praisenumber_id)
         TextView praisenumber_id;
        @BindView(R.id.viewnumber__id)
         TextView viewnumber__id;

        public VHThree(View v) {
            ButterKnife.bind(this,v);

        }
    }

    //大图
     static class VHBigPic {
        @BindView(R.id.newsitem_title)
         TextView newsitem_title;
        @BindView(R.id.nli_foot)
         ImageView nli_foot;
        @BindView(R.id.newsitem_copyfrom)
         TextView newsitem_copyfrom;
        @BindView(R.id.praisenumber_id)
         TextView praisenumber_id;

        @BindView(R.id.newsitem_time)
         TextView newsitem_time;
        @BindView(R.id.newsitem_img)
         ImageView newsitem_img;
        @BindView(R.id.viewnumber__id)
         TextView viewnumber__id;



         VHBigPic(View v) {
            ButterKnife.bind(this,v);
        }
    }

    //左边图片，右title，评论，时间，脚标
     static class VHLeftPic {
        @BindView(R.id.newsitem_title)
         TextView newsitem_title;
        @BindView(R.id.nli_foot)
         ImageView nli_foot;
        @BindView(R.id.newsitem_copyfrom)
         TextView newsitem_copyfrom;
        @BindView(R.id.praisenumber_id)
         TextView praisenumber_id;
        @BindView(R.id.newsitem_time)
         TextView newsitem_time;
        @BindView(R.id.newsitem_img)
         ImageView newsitem_img;
        @BindView(R.id.viewnumber__id)
         TextView viewnumber__id;
        @BindView(R.id.tv_praise_id)
         ImageView tv_praise_id;
        @BindView(R.id.ll_praise_id)
         LinearLayout ll_praise_id;


        public VHLeftPic(View v) {
            ButterKnife.bind(this,v);
        }
    }

    //左边图片，右title，评论，时间，脚标
     static class VHNOPic {
        @BindView(R.id.newsitem_title)
         TextView newsitem_title;
        @BindView(R.id.nli_foot)
         ImageView nli_foot;
        @BindView(R.id.newsitem_copyfrom)
         TextView newsitem_copyfrom;
        @BindView(R.id.praisenumber_id)
         TextView praisenumber_id;

        @BindView(R.id.newsitem_time)
         TextView newsitem_time;
        @BindView(R.id.viewnumber__id)
         TextView viewnumber__id;

        public VHNOPic(View v) {
            ButterKnife.bind(this,v);
        }
    }
}
